package com.live.dengdengtv.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.live.dengdengtv.R;
import com.live.dengdengtv.adapter.MsgAdapter;
import com.live.dengdengtv.bean.AppConstants;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.bean.gson.ConversationCard;
import com.live.dengdengtv.bean.gson.PersonInfoItem;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.view.SlidingTabLayout;
import com.tencent.TIMMessage;

import java.util.ArrayList;
import java.util.List;

/**
 */
public class MsgFragment_bak extends BaseFragment {

    private static final String TAG = MsgFragment_bak.class.getName();
    private ViewPager mViewPager;
    private SlidingTabLayout mSlidingTabLayout;

    private ArrayList<PersonInfoItem> friendList = new ArrayList<PersonInfoItem>();

    private StrangerMsgFrag strangerMsgFrag = new StrangerMsgFrag();

    public void setOnItemClickListener(ConversationItemClick onItemClickListener){
        strangerMsgFrag.setOnItemClickListener(onItemClickListener);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_msg, container, false);

        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mViewPager.setAdapter(new MsgFragmentPagerAdapter(getChildFragmentManager()));

        initSlidingTabLayout(view);

//        getFriendList();
        return view;
    }

    private void getFriendList(){
        requestData(NetTaskList.friend_list, AppInfo.getHeadInfo(),1);
    }

    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if (id == 1) {
            friendList.clear();
            if(response.result == 0){
                JsonArray array = response.data.getAsJsonArray();
                for (int i=0; i<array.size() ;i++){
                    PersonInfoItem liveItem = new Gson().fromJson(array.get(i).getAsJsonObject(), PersonInfoItem.class);
                    friendList.add(liveItem);
                }
            }
        }
        return false;
    }


    private List<TIMMessage> conversationList = new ArrayList<>();
    private List<TIMMessage> conversationFriend = new ArrayList<>();
    private List<TIMMessage> conversationStranger = new ArrayList<>();
    private List<ConversationCard> conCardList;
    private void getConversationCard(){
        SharedPreferences sp = getActivity().getSharedPreferences(AppConstants.SP_CONVERSATIOM, Context.MODE_PRIVATE);
        String conJson = sp.getString(AppConstants.KEY_CONVERSATIOM,"");
        conCardList = new Gson().fromJson(conJson, conCardList.getClass());
    }


    private void initSlidingTabLayout(View view) {
        mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setCustomTabView(R.layout.hall_tabtv, R.id.tab_tv);

        final float density = getResources().getDisplayMetrics().density;
        mSlidingTabLayout.setNavigationThick((int) (density*5));
        mSlidingTabLayout.setDefaultTextColor(0xFFECE8E9);
        mSlidingTabLayout.setSelectedTextColor(0xFFED242C);

        mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return 0xFFED242C;
            }
            @Override
            public int getDividerColor(int position) {
                return 0x00FFFFFF;
            }
        });

        mSlidingTabLayout.setViewPager(mViewPager);
    }

    private FragmentStopLis fragmentStopLis;
    public void setOnStopLis(FragmentStopLis fragmentStopLis){
        this.fragmentStopLis = fragmentStopLis;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(fragmentStopLis!=null){
            fragmentStopLis.onFragmentStop();
            fragmentStopLis = null;
        }
    }



    private class MsgFragmentPagerAdapter extends FragmentPagerAdapter {
        private String[] titles = new String[]{"好友","未关注的人","通知"};

        MsgFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment;
            if(i == 0){
                fragment = new Fragment();
            }else if(i == 1){
                fragment = strangerMsgFrag;
            }else {
                fragment = new Fragment();
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

















    @Deprecated
    static class ContentPagerAdapter extends PagerAdapter{
        private String[] titles = new String[]{"好友","未关注的人","通知"};
        private Context context;
        public ContentPagerAdapter(Context context) {
            this.context = context;
        }
        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view;
            if(position == 0){
                view = getFriendView();
            }else if(position == 1){
                view = getStrangerView();
            }else {
                view = getNoticeView();
            }
            container.addView(view);
            return view;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        private View getFriendView() {
            ListView listView = new ListView(context);
            ViewPager.LayoutParams lp = new ViewPager.LayoutParams();
            lp.height = lp.MATCH_PARENT;
            lp.width = lp.MATCH_PARENT;
            listView.setLayoutParams(lp);
            listView.setDivider(new ColorDrawable(0xFFE5E5E5));
            listView.setDividerHeight(2);

            listView.setAdapter(new MsgAdapter(1,context));
            return listView;
        }

        private View getStrangerView() {
            ListView listView = new ListView(context);
            ViewPager.LayoutParams lp = new ViewPager.LayoutParams();
            lp.height = lp.MATCH_PARENT;
            lp.width = lp.MATCH_PARENT;
            listView.setLayoutParams(lp);
            listView.setDivider(new ColorDrawable(0xFFE5E5E5));
            listView.setDividerHeight(2);

            listView.setAdapter(new MsgAdapter(2,context));

            return listView;
        }
        private View getNoticeView() {
            ListView listView = new ListView(context);
            ViewPager.LayoutParams lp = new ViewPager.LayoutParams();
            lp.height = lp.MATCH_PARENT;
            lp.width = lp.MATCH_PARENT;
            listView.setLayoutParams(lp);
            listView.setDivider(new ColorDrawable(0xFFE5E5E5));
            listView.setDividerHeight(2);

            listView.setAdapter(new BaseAdapter() {
                @Override
                public int getCount() {
                    return 5;
                }

                @Override
                public Object getItem(int position) {
                    return position;
                }

                @Override
                public long getItemId(int position) {
                    return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    LinearLayout ll = (LinearLayout) View.inflate(context, R.layout.card_notice, null);
                    return ll;
                }
            });
            return listView;
        }

    }
}
